package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ChannelPageReqDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;

import com.heima.model.common.pojos.wemedia.WmChannel;
import com.heima.model.common.pojos.wemedia.WmNews;
import com.heima.wemedia.mapper.ChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.ChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, WmChannel> implements ChannelService {
    @Autowired
    private WmNewsMapper wmNewsMapper;
    /**
     * 频道分页列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult pageQuery(ChannelPageReqDto dto) {
        //1.检查参数
        if (dto==null)
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        dto.checkParam();
        //2.组装条件wrapper
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.<WmChannel>lambdaQuery();
        //2.1可以按照状态进行精确查找（1：启用   true           0：禁用   false）
        if (dto.getStatus()!=null){
            wrapper.eq(WmChannel::getStatus,dto.getStatus());
        }
        //2.2按照频道名称模糊查询
        if (dto.getName()!=null){
            wrapper.like(WmChannel::getName,dto.getName());
        }
        //2.3查询需要按照创建时间倒序查询
        wrapper.orderByDesc(WmChannel::getCreatedTime);
        //3.构建分页
        Page<WmChannel> page=new Page<>(dto.getPage(),dto.getSize());
        //4.构建分页page
        page(page,wrapper);
        //封装结果返回
        PageResponseResult result = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 保存频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        //1.参数校验
        if (wmChannel==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断名字是否重复
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmChannel::getName, wmChannel.getName());
        int count=count(wrapper);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //4.设置默认属性值
        wmChannel.setOrd(1);
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(true);

        //3.保存频道
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    /**
     * 修改频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //1.参数校验
        if (wmChannel==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否被引用
       Integer count= wmNewsMapper.selectCount(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId,wmChannel.getId()));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道被引用则不能禁用");
        }
        //3.更新数据
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //1.参数校验
        if (id==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断是否被禁用
        WmChannel wmChannel = getById(id);
        if (wmChannel.getStatus()){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道未禁用，无法删除");
        }
        //3.删除数据
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
